<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.name" placeholder="角色名称"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 工具条 -->
    <el-row style="margin-bottom: 10px">
      <el-button type="info" size="small" @click="addRole()" v-if="hasAuthority('role.add')">添加</el-button>
      <el-button type="danger" size="small" @click="removeByIds()" v-if="hasAuthority('role.remove')">批量删除</el-button>
    </el-row>

    <!-- 讲师列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55"/>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="name" label="角色名称" align="center"/>

      <el-table-column prop="code" label="角色Code" align="center"/>

      <el-table-column prop="remark" label="角色备注" align="center"/>

      <el-table-column prop="createdDate" label="创建时间" width="180" align="center"/>


      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/acl/role/distribution/'+scope.row.id">
            <el-tooltip class="item" effect="dark" content="分配权限" placement="bottom">
              <el-button type="info" size="mini" icon="el-icon-info" v-if="hasAuthority('role.acl')"></el-button>
            </el-tooltip>
          </router-link>
          <router-link :to="'/acl/role/update/'+scope.row.id">
            <el-tooltip class="item" effect="dark" content="修改" placement="bottom">
              <el-button type="primary" size="mini" icon="el-icon-edit" v-if="hasAuthority('role.update')"></el-button>
            </el-tooltip>
          </router-link>
          <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)" v-if="hasAuthority('role.remove')"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import roleApi from '@/api/acl/role'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // 讲师列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      multipleSelection: [] // 批量选择中选择的记录列表
    }
  },

  created() {
    this.fetchData()
  },

  methods: {

    changeSize(size) {
      this.limit = size
      this.fetchData(1)
    },

    addRole() {
      this.$router.push({path: '/acl/role/add'})
    },

    fetchData(page = 1) {
      this.page = page
      roleApi.findPage(this.page, this.limit, this.searchObj).then(
        response => {
          this.list = response.data.items
          this.total = response.data.total
          this.listLoading = false
        }
      )
    },

    resetData() {
      this.searchObj = {}
      this.fetchData()
    },

    removeById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return roleApi.removeById(id)
      }).then((response) => {
        this.fetchData(this.page)
        if (response.code === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },

    removeByIds() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录!'
        })
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        return roleApi.removeByIds(ids)
      }).then((response) => {
        this.fetchData(this.page)
        if (response.code === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

  }
}
</script>

